package com.hanrui.android.net.presenter.addresslist;


import android.content.Context;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.AddressListBean;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.addresslist.AddressContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * 通讯录（人员）
 * Created by Administrator on 2018/3/9.
 */

public class AddressPresenter extends BasePresenter<AddressContract.View> implements AddressContract.Presenter {

    @Inject
    public AddressPresenter(){}

    @Override
    public void companyList(final Context context,String companyId,String name) {
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .companyList(companyId,name)
                .compose(RxSchedulers.<BaseBean<AddressListBean>>applySchedulers())
                .compose(mView.<BaseBean<AddressListBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<AddressListBean>>() {
                    @Override
                    public void accept(BaseBean<AddressListBean> response) throws Exception {
                        if (response.getCode() == 1) {
                            mView.companyList(response.getData().getNoAttendList());
                        }else if (response.getCode() == 201) {
                            LoginActivity.start(context);
                        } else {
                            mView.showFaild(String.valueOf(response.getMessage()));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild(throwable.getMessage());
                    }
                });

    }
}
